package com.example.test1;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.nfc.NfcAdapter;
import android.os.BatteryManager;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class MainService extends Service {
    
    private NotificationManager mNotiManager;

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }
        
    @Override
    public void onStart(Intent intent, int startId) {
        Log.i("xyzi", "Service onStart()");        
        //반드시 호출해줘야 함
        super.onStart(intent, startId);
        
        /*** add actions to filter ***/
        IntentFilter filter = new IntentFilter();
    	 filter.addAction(Intent.ACTION_BATTERY_CHANGED); //Charger, USB
    	 filter.addAction(NfcAdapter.ACTION_NDEF_DISCOVERED); //Nfc
    	 filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); //Bluetooth
    	 filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); //Wifi
    	 filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); //Wifi, NetWork
    	 filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
    	 filter.addAction(Intent.ACTION_HEADSET_PLUG);
    	 filter.addAction(Intent.ACTION_DOCK_EVENT);
    	
    	 filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
    	 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
    	 
    	 filter.addAction(ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
    	 filter.addAction(Intent.ACTION_DOCK_EVENT);

    	 registerReceiver(mBR, filter);
    	 /*** add action to filter ***/
    	 
    	 mNotiManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    	 Notification noti = new Notification(R.id.menu_settings,"감시중",System.currentTimeMillis());
    	 
    	 mNotiManager.notify(1, noti);
    }
    
    @Override
    public void onDestroy() {
        Log.i("xyzi", "Service onDestory()");
        unregisterReceiver(mBR);
        super.onDestroy();
    }
    
    
    BroadcastReceiver mBR = new BroadcastReceiver() {
    	int count = 0; //receive count
    	@Override
    	public void onReceive(Context context, Intent intent) {
    		String action = intent.getAction();
    		
    		count++;
    		
    		if(action.equals(ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED)) {
    			ConnectivityManager CM = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    			
    			if(CM.getBackgroundDataSetting())
    				Toast.makeText(context, "BACKGROUND_DATA_SETTING_CHANGED to ON", Toast.LENGTH_LONG).show();
    			else
    				Toast.makeText(context, "BACKGROUND_DATA_SETTING_CHANGED to OFF", Toast.LENGTH_LONG).show();
    		}
    		
    		if(action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) { //UIS need to be rebuilt
    			Toast.makeText(context, "ACTION_CONFIGURATION_CHANGED", Toast.LENGTH_LONG).show();
    		}
    		
    		if(action.equals(intent.ACTION_DOCK_EVENT)) {
    			int dockState = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, 0);
    		
    			switch(dockState) {
    			case Intent.EXTRA_DOCK_STATE_UNDOCKED:
    				///Toast.makeText(context, "Dock Disconnected", Toast.LENGTH_LONG).show();
    				break;
    			case Intent.EXTRA_DOCK_STATE_CAR:
    			case Intent.EXTRA_DOCK_STATE_DESK:
    				///Toast.makeText(context, "Dock Connected", Toast.LENGTH_LONG).show();
    				break;
    			}
    		}
    		
    		if(action.equals(intent.ACTION_HEADSET_PLUG)) {
    			int headSetState = intent.getIntExtra("state", 0);
    			int hasMicrophone = intent.getIntExtra("microphone", 0);
    			switch(headSetState) {
    			case 0:
    				//Toast.makeText(context, "HeadSet Unplugged", Toast.LENGTH_LONG).show();
    				break;
    			case 1:
    				switch(hasMicrophone) {
    				case 0:
    					///Toast.makeText(context, "HeadSet without Microphone plugged", Toast.LENGTH_LONG).show();
    					break;
    				case 1:
    					///Toast.makeText(context, "HeadSet with Microphone plugged", Toast.LENGTH_LONG).show();
    					break;
    				}
    			}
    		}
    		
    		if(action.equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
    			int ringMode = intent.getIntExtra(AudioManager.EXTRA_RINGER_MODE, -1); // set defaultvalue as -1
    			switch(ringMode) {
    			case AudioManager.RINGER_MODE_NORMAL:
    				///Toast.makeText(context, "RINGER_NORMAL_MODE", Toast.LENGTH_LONG).show();
    				break;
    			case AudioManager.RINGER_MODE_SILENT:
    				///Toast.makeText(context, "SILENT MODE", Toast.LENGTH_LONG).show();
    				break;
    			case AudioManager.RINGER_MODE_VIBRATE:
    				///Toast.makeText(context, "VIBRATE MODE", Toast.LENGTH_LONG).show();
    				break;
    			default:
    				//Toast.makeText(context, "RINGER_MODE_CHANGED_ACTION : UNKNOWN", Toast.LENGTH_LONG).show();
    			}
    		}
    		
    		if(action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) { /***it works well***/
    			int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN);
    			switch(wifiState){
    			case WifiManager.WIFI_STATE_ENABLED:
    				///Toast.makeText(context, "Wifi Enabled", Toast.LENGTH_LONG).show();
    				break;
    			case WifiManager.WIFI_STATE_DISABLED:
    				///Toast.makeText(context, "Wifi Disabled", Toast.LENGTH_LONG).show();
    				break;
    			default:
    				//Toast.makeText(context, "WIFI_STATE_CHANGE_ACTION : UNKNOWN", Toast.LENGTH_LONG).show();
    			}
    		}
    		
    		if(action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) { /***MAC addr problem. WIFI �������� Ȯ�� �ʿ�.***/
    			NetworkInfo netInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
    			if(netInfo.getDetailedState() == NetworkInfo.DetailedState.CONNECTED){
    				WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
        			WifiInfo wifiInfo = wifiManager.getConnectionInfo(); /**** it makes problem ***/
        			String bSSID = wifiInfo.getBSSID();
        			String macAddr = wifiInfo.getMacAddress(); //phone's MAC addr
        			///Toast.makeText(context, "Wifi Connected.", Toast.LENGTH_LONG).show();
    				///Toast.makeText(context, "Network Connected.\n SSID : " + bSSID + "\n MAC addr : " + macAddr, Toast.LENGTH_LONG).show();
    			}
    		}
    	    	
    		
    		if(action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) { //test
	    		Toast.makeText(context, "CONNECTIVITY_ACTION", Toast.LENGTH_LONG).show();
    		}
    	    		
     		if(action.equals(intent.ACTION_DOCK_EVENT)) {
  	   			int dockState = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, 0);
      			switch(dockState) {
        			case Intent.EXTRA_DOCK_STATE_UNDOCKED:
       				///Toast.makeText(context, "Dock Disconnected", Toast.LENGTH_LONG).show();
        				break;
        			case Intent.EXTRA_DOCK_STATE_CAR:
        			case Intent.EXTRA_DOCK_STATE_DESK:
        				///Toast.makeText(context, "Dock Connected", Toast.LENGTH_LONG).show();
        				break;
      			}
     		}
    	    		
    	    if(action.equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
    			int ringMode = intent.getIntExtra(AudioManager.EXTRA_RINGER_MODE, -1); // set defaultvalue as -1
	   			switch(ringMode) {
	   				case AudioManager.RINGER_MODE_NORMAL:
    	    			///Toast.makeText(context, "RINGER_NORMAL_MODE", Toast.LENGTH_LONG).show();
    	    			break;
    	    		case AudioManager.RINGER_MODE_SILENT:
    	    	   		///Toast.makeText(context, "SILENT MODE", Toast.LENGTH_LONG).show();
    	    	   		break;
    	   		   	case AudioManager.RINGER_MODE_VIBRATE:
    	   		   		///Toast.makeText(context, "VIBRATE MODE", Toast.LENGTH_LONG).show();
    	   		   		break;
    	   		   	default:
        		   		//Toast.makeText(context, "RINGER_MODE_CHANGED_ACTION : UNKNOWN", Toast.LENGTH_LONG).show();
	   			}
    	    }
    	    		    		    		
      		if(action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) { /***it works well***/
 	   			int blueState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
      			switch(blueState){
         			case BluetoothAdapter.STATE_ON:
   	     				///Toast.makeText(context, "Bluetooth Enabled", Toast.LENGTH_LONG).show();
  	   					break;
      	   			case BluetoothAdapter.STATE_OFF:
     	   				///Toast.makeText(context, "Bluetooth Disabled", Toast.LENGTH_LONG).show();
     	   				break;
      	  			default:
        	 			//Toast.makeText(context, "Bluetooth ACTION_STATE_CHANGED : UNKNOWN", Toast.LENGTH_LONG).show();
         		}
      		}
      		/*
    	      		if(action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)) {
    	      			int bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR);
    	      			switch(bondState){
    	        			case BluetoothDevice.BOND_BONDED:
    	      				Toast.makeText(context, "Bluetooth Device Bonded", Toast.LENGTH_LONG).show();
    	       			}
    	      			Toast.makeText(context, "Bluetooth Device Bond State Changed", Toast.LENGTH_LONG).show();
    	       		}
    	    */
    	     		
    	    if (action.equals(Intent.ACTION_BATTERY_CHANGED)) { /***it works well***/
    	    	int plug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
    	   	 	switch(plug){
    	   	 	case BatteryManager.BATTERY_PLUGGED_AC:
    	   	    	///Toast.makeText(context, "Connected to a AC", Toast.LENGTH_LONG).show();
    	  			break;
      			case BatteryManager.BATTERY_PLUGGED_USB:
      				///Toast.makeText(context, "Connected to a PC", Toast.LENGTH_LONG).show();
    	    		break;
      			default:
        			//Toast.makeText(context, "ACTION_BATTERY_CHANGED : UNKNOWN", Toast.LENGTH_LONG).show();
      			}
       		}
    	    		
    	    if (action.equals(NfcAdapter.ACTION_TAG_DISCOVERED)) { // not tested yet. need intent-filter in Manifest?
    	    		///Toast.makeText(context, "NFC tag is detected", Toast.LENGTH_LONG).show();
    	     }
    	}
    };
}
